<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/06/25
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Tpmecmscom;
use think\Db;
use think\Config;

/**
 * 提成记录
 */
class Commission extends Tpmecmscom
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['add_commission'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    /**
     * 添加提成
     *
     * @param int $order_id 订单id
     * 
     * @return void
     */
    public function add_commission($order_id,$bug = null)
    {
        $res = Db::name('commission')->where('order_id',$order_id)->find();
        if($res){
            $this->error('已有该提成');
        }
        $order = $this->GetOrderData($order_id);
        if($order['pid']>0){
            $data['order_id'] = $order_id;
            $data['commission'] = $order['commission'];
            $data['price'] = $order['price'];
            $data['goods_id'] = $order['goods_id'];
            $data['time'] = time();
            $data['uid'] = $order['uid'];
            $data['pid'] = $otder['pid'];
            $res = Db::name('commission')->insert($data);

        }
        if($bug)
        $this->success('ok',$order);
        // if($order[''])
    }


    /**
     * 获取提成列表📃
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function get_commission_list($page=1,$limit=10)
    {
        $list = Db::name('commission')
                ->where('pid',$this->auth->id)
                ->order('time DESC')
                ->page($page,$limit)
                ->field([ '*,FROM_UNIXTIME(time,"%Y-%m-%d %H:%i:%s") as date'])//create
                ->select();

        $this->success('ok',$list);
    }

  


    /**
     * 获取提成信息📋
     *
     * @param int $id id
     * @return void
     */
    public function get_commission_data($id)
    {
        $data['id'] = $id;
        $data['pid'] = $this->auth->id;
        $res = Db::name('commission')
            ->where($data)
            ->find();

        $this->success('ok',$res);
    }

    /**
     * 获取提成钱信息
     *
     * @return void
     */
    public function get_money_data()
    {
        $data['total'] = $this->GetCommissionTotal($this->auth->id);//总
        $data['out'] = $this->GetCommissionOut($this->auth->id);
        $data['money'] = $data['total'] - $data['out'];
        $ltder = Db::name('ltder')->where('uid',$this->auth->id)->find();
        if($ltder){
            $data['look_switch'] = Db::name('ltd')->where('id',$ltder['ltd_id'])->value('look_switch');
        } else{
            $data['look_switch'] = 1;
        }
        
        $this->success('ok',$data);
    }


    /**
     * 申请提现
     *
     * @param int $money 金额
     * @return void
     */
    public function add_out($money)
    {
        $data['uid'] = $this->auth->id;
        $data['status'] = '1';
        $res = Db::name('out')
            ->where($data)
            ->find();
        if($res){
            $this->error('已有申请提现，请同意后在申请');
        }



        $data['money'] = $money;
        $data['time'] = time();
        $res = Db::name('out')->insert($data);
        if($res){
            $this->success('提交成功');
        }
    }


    /**
     * 获取提现列表📃
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function get_out_list($page=1,$limit=10)
    {
        $list = Db::name('out')
                ->where('uid',$this->auth->id)
                ->order('time DESC')
                ->page($page,$limit)
                ->field([ '*,FROM_UNIXTIME(time,"%Y-%m-%d %H:%i:%s") as date'])//create
                ->select();

        $this->success('ok',$list);
    }
}
